import request, { ApiResponse } from './request'
import type { UserInfo } from '@/types/user'

// 验证码响应
export interface CaptchaResponse {
  id: string
  svg: string
}

// 登录接口参数
export interface LoginParams {
  username: string
  password: string
  captchaId: string
  captcha: string
}

// 登录响应
export interface LoginResponse {
  user: {
    id: string
    username: string
    nickname: string
    email: string
    phone: string
    is_admin: number
    status: number
  }
  token: string
}

// 生成验证码
export const getCaptcha = (): Promise<ApiResponse<CaptchaResponse>> => {
  return request.post<CaptchaResponse>('/auth/captcha', {})
}

// 登录
export const login = (data: LoginParams): Promise<ApiResponse<LoginResponse>> => {
  return request.post<LoginResponse>('/auth/login', data)
}

// 获取用户信息
export const getUserInfo = (): Promise<ApiResponse<UserInfo>> => {
  return request.post<UserInfo>('/auth/current')
}

// 退出登录
export const logout = (): Promise<ApiResponse> => {
  return request.post('/auth/logout')
}

